package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"time"
)

type StuScore struct {
	XSBM string  `json:"xsbm" form:"xsbm" gorm:"column:xsbm;comment:'学生编码';type:varchar(50);index;"`
	XMBM int     `json:"xmbm" form:"xmbm" gorm:"column:xmbm;comment:'项目编码';"`
	YDXM string  `json:"ydxm" form:"ydxm" gorm:"column:ydxm;comment:'运动项目';type:varchar(50);"`
	XMCS string  `json:"xmcs" form:"xmcs" gorm:"column:xmcs;comment:'项目次数';type:varchar(50);"`
	CJSJ string  `json:"cjsj" form:"cjsj" gorm:"column:cjsj;comment:'成绩时间';type:varchar(50);"`
	YDSJ float64 `json:"ydsj" form:"ydsj" gorm:"column:ydsj;comment:'运动数据';"`
}

func send(s []StuScore) {
	for i := 0; i < len(s); i++ {
		url := "http://192.168.30.128:8080/api/score/receiveScore"

		data, err := json.Marshal(s[i])
		var jsonStr = []byte(data)
		fmt.Println("jsonStr", jsonStr)
		fmt.Println("new_str", bytes.NewBuffer(jsonStr))

		req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
		// req.Header.Set("X-Custom-Header", "myvalue")
		req.Header.Set("Content-Type", "application/json")

		client := &http.Client{}
		resp, err := client.Do(req)
		if err != nil {
			panic(err)
		}
		defer resp.Body.Close()

		fmt.Println("status", resp.Status)
		fmt.Println("response:", resp.Header)
		body, _ := ioutil.ReadAll(resp.Body)
		fmt.Println("response Body:", string(body))
		time.Sleep(1 * time.Second)
	}

}

func sendJSON() {
	s0 := StuScore{
		XSBM: "2070436006",
		XMBM: 2,
		YDXM: "50米跑",
		XMCS: "1",
		CJSJ: "2022-04-23 15:24:45",
		YDSJ: 15.10,
	}
	s1 := StuScore{
		XSBM: "2070436006",
		XMBM: 2,
		YDXM: "50米跑",
		XMCS: "2",
		CJSJ: "2022-04-23 15:24:45",
		YDSJ: 15.65,
	}
	s2 := StuScore{
		XSBM: "2070436006",
		XMBM: 2,
		YDXM: "50米跑",
		XMCS: "3",
		CJSJ: "2022-04-23 15:24:45",
		YDSJ: 15.39,
	}
	s3 := StuScore{
		XSBM: "2070436006",
		XMBM: 2,
		YDXM: "50米跑",
		XMCS: "4",
		CJSJ: "2022-04-23 15:24:45",
		YDSJ: 14.97,
	}
	s4 := StuScore{
		XSBM: "2070436006",
		XMBM: 2,
		YDXM: "50米跑",
		XMCS: "5",
		CJSJ: "2022-04-23 15:24:45",
		YDSJ: 14.89,
	}
	s5 := StuScore{
		XSBM: "2070436006",
		XMBM: 2,
		YDXM: "50米跑",
		XMCS: "6",
		CJSJ: "2022-04-23 15:24:45",
		YDSJ: 15.12,
	}
	s6 := StuScore{
		XSBM: "2070436006",
		XMBM: 2,
		YDXM: "50米跑",
		XMCS: "7",
		CJSJ: "2022-04-23 15:24:45",
		YDSJ: 15.38,
	}
	s7 := StuScore{
		XSBM: "2070436006",
		XMBM: 2,
		YDXM: "50米跑",
		XMCS: "8",
		CJSJ: "2022-04-23 15:24:45",
		YDSJ: 15.59,
	}
	s8 := StuScore{
		XSBM: "2070436006",
		XMBM: 2,
		YDXM: "50米跑",
		XMCS: "9",
		CJSJ: "2022-04-23 15:24:45",
		YDSJ: 15.13,
	}
	s9 := StuScore{
		XSBM: "2070436006",
		XMBM: 2,
		YDXM: "50米跑",
		XMCS: "10",
		CJSJ: "2022-04-23 15:24:45",
		YDSJ: 14.94,
	}
	s := []StuScore{s0, s1, s2, s3, s4, s5, s6, s7, s8, s9}
	send(s)
}
